package view;

import factory.*;
import product.AutoInsurance;

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class ClientGUI extends JFrame {
    private static final long serialVersionUID = -5077177071790319134L;
    
    private JSplitPane bigSplitPane;
    private JScrollPane showInfoPane;
    private JPanel btnPanel;
    private JComboBox<String> cmbInsuranceType;
    private JLabel lblInsureType;
    private JTextArea txtForInfo;

    public static final String SHOW = "Show Info";
    public static final String EXIT = "Exit";
    public static final String BODYINJURE = "Body Injur Liability";
    public static final String COLLISION = "Collision Coverage";
    public static final String PERSONINJURE = "Personal Injury Protection";
    public static final String COMPREHENSIVE = "Comprehensive Coverage";
    public static final String LUXURYINSUR = "Luxury Insurance";

    public ClientGUI() {
        super("Factory Method Pattern- Auto Insurance. ");
        new Dimension(130, 100);
        setUpChoicePanel();
        setUpScrollPanes();
    }

    private void setUpChoicePanel() {

        cmbInsuranceType = new JComboBox<String>();
        cmbInsuranceType.addItem(BODYINJURE);
        cmbInsuranceType.addItem(COLLISION);
        cmbInsuranceType.addItem(PERSONINJURE);
        cmbInsuranceType.addItem(COMPREHENSIVE);
        cmbInsuranceType.addItem(LUXURYINSUR);

        lblInsureType = new JLabel("Insurance Types");

        // Create the open button
        JButton openButton = new JButton(SHOW);
        openButton.setMnemonic(KeyEvent.VK_S);
        JButton exitButton = new JButton(EXIT);
        exitButton.setMnemonic(KeyEvent.VK_X);

        ButtonListener btnListener = new ButtonListener();

        // add action Listener
        openButton.addActionListener(btnListener);
        exitButton.addActionListener(btnListener);

        btnPanel = new JPanel();

        // ------------------------------------------------
        GridBagLayout gridbag = new GridBagLayout();
        btnPanel.setLayout(gridbag);
        GridBagConstraints gbc = new GridBagConstraints();

        btnPanel.add(lblInsureType);
        btnPanel.add(cmbInsuranceType);
        btnPanel.add(openButton);
        btnPanel.add(exitButton);

        gbc.insets.top = 5;
        gbc.insets.bottom = 5;
        gbc.insets.left = 5;
        gbc.insets.right = 5;

        gbc.gridx = 0;
        gbc.gridy = 0;
        gridbag.setConstraints(lblInsureType, gbc);
        gbc.gridx = 1;
        gbc.gridy = 0;
        gridbag.setConstraints(cmbInsuranceType, gbc);

        gbc.insets.left = 2;
        gbc.insets.right = 2;
        gbc.insets.top = 15;
        gbc.gridx = 0;
        gbc.gridy = 5;
        gridbag.setConstraints(openButton, gbc);
        gbc.anchor = GridBagConstraints.WEST;
        gbc.gridx = 1;
        gbc.gridy = 5;
        gridbag.setConstraints(exitButton, gbc);
        // -----------------------------------------------
    }

    private void setUpScrollPanes() {

        txtForInfo = new JTextArea("Auto insurance information will be shown here.", 20, 30);
        txtForInfo.setFont(new Font("Helvetica", Font.BOLD, 15));

        txtForInfo.setLineWrap(true);
        txtForInfo.setBackground(Color.pink);

        showInfoPane = new JScrollPane(txtForInfo);

        bigSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, showInfoPane, btnPanel);
        bigSplitPane.setDividerLocation(220);

        getContentPane().add(bigSplitPane);
        setSize(new Dimension(500, 300));
        setVisible(true);
    }

    class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent ae) {

            if (ae.getActionCommand().equals(EXIT)) {
                System.exit(1);
            }

            if (ae.getActionCommand().equals(SHOW)) {

                String type = (String) cmbInsuranceType.getSelectedItem();
                PolicyProducer pp = null;

                switch (type) {
                case BODYINJURE:
                    pp = new BodyPolicy();
                    break;
                case COLLISION:
                    pp = new CollPolicy();
                    break;
                case PERSONINJURE:
                    pp = new PersonPolicy();
                    break;
                case COMPREHENSIVE:
                    pp = new ComPolicy();
                    break;
                case LUXURYINSUR:
                    pp = new LuxuryPolicy();
                    break;
                }

                AutoInsurance ai = pp.getAutoObj();
                String desc = ai.getInfo();
                txtForInfo.setText(desc);
            }
        }
    }

    public static void main(String args[]) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
                | UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }

        ClientGUI frame = new ClientGUI();
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });

        frame.setSize(500, 420);
        frame.setVisible(true);
    }
}
